package com.ruoyi.mateproduction.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateChildren;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.domain.MateTransferModel;
import com.ruoyi.mateproduction.service.MateTransferService;
import com.ruoyi.mateproduction.service.MtProductionService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.service.MateWarehousingService;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */

@RestController
@RequestMapping("/mateTransfer")
public class MateTransferController extends BaseController {

    @Autowired
    MateTransferService mateTransferService;

    @Autowired
    MtProductionService mtProductionService;

    @Autowired
    MateWarehousingService mateWarehousingService;

    @Autowired
    ProductMapper productMapper;

    /**
     * 保存生产工序传递卡    yi
     */
    @Log(title = "生产工序传递卡", businessType = BusinessType.INSERT)
    @PostMapping("/addMateTransfer")
    public AjaxResult addMateTransfer(@RequestBody MateTransfer mateTransfer) {
        mateTransfer.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = mateTransferService.addMateTransfer(mateTransfer);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /*
   查询所有的原材料信息
    */
    @GetMapping("/mateProductList")
    @ResponseBody
    public List<ProductRelation> mateProductList(@RequestParam(value = "productId", required = false) Long productId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("deptId", SecurityUtils.getDeptId());
        resMap.put("productId", productId);
        List<ProductRelation> productRelations = productMapper.selectMateProductId(resMap);
        return productRelations;
    }

    /*
  查询库存的原材料信息
   */
    @GetMapping("/selectWarehousing")
    @ResponseBody
    public List<MateWarehousing> selectWarehousing(@RequestParam(value = "mateProductId", required = false) Long mateProductId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("mateProductId", mateProductId);
        resMap.put("deptId", SecurityUtils.getDeptId());
        List<MateWarehousing> warehousings = mateWarehousingService.selectMateWarehousing(resMap);
        return warehousings;
    }

    /**
     * 查询自己公司旗下自己生产的产品
     */
    @GetMapping("/tableSelect")
    @ResponseBody
    public JSONObject tableSelect(@RequestParam(value = "productType", required = false) String productType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        HashMap map = new HashMap();
        map.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (productType != null & !"".equals(productType)) {
            map.put("productType", productType);
        }

        List<ProductModel> productModels = mtProductionService.selectTableList(map);
        jsonObject.put("count", productModels.size());
        JSONArray jsonArray = new JSONArray();
        Long amount = 0L;
        for (int i = 0; i < productModels.size(); i++) {
            ProductModel productModel = productModels.get(i);
            if (productModel.getSalesUnit().equals("盒")) {
                String b = productModel.getWarehousingNum();
                int c = Integer.valueOf(b);
                amount = c / productModel.getFactor();
            } else {
                amount = Long.valueOf(productModel.getWarehousingNum());
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("modelCode", productModel.getModelCode());
            jsonObject1.put("productId", productModel.getProduct().getProductId());
            jsonObject1.put("productName", productModel.getProduct().getProductName());
            jsonObject1.put("productType", productModel.getProductType());
            jsonObject1.put("unit", productModel.getSalesUnit());
            jsonObject1.put("manufacturerName", productModel.getManufacturerName());
            jsonObject1.put("warehousingNum", amount);
            jsonObject1.put("amount", productModel.getAmount());
            jsonObject1.put("factor", productModel.getFactor());
            jsonObject1.put("scopeId", productModel.getScopeId());
            jsonArray.add(jsonObject1);
        }
        jsonObject.put("data", jsonArray);
        return jsonObject;
    }

    /**
     * 销售订单管理list
     */
    @GetMapping("/productList")
    @ResponseBody
    public TableDataInfo productList(@RequestParam(value = "productName", required = false) String productName,
                                     @RequestParam(value = "productType", required = false) String productType) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        if (productName != null) {
            map.put("productName", productName);
        }
        if (productType != null) {
            map.put("productType", productType);
        }
        List<ProductModel> list = mtProductionService.selectProductList(map);
        return getDataTable(list);
    }

    /**
     * 查询生产工序传递卡
     */
    @GetMapping("/mateTransferList")
    @ResponseBody
    public TableDataInfo mateTransferList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                          @RequestParam(value = "endTime", required = false) String endTime) {
        MateTransfer mateTransfer = new MateTransfer();
        mateTransfer.setBeginTime(beginTime);
        mateTransfer.setEndTime(endTime);
        startPage();
        mateTransfer.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateTransfer> list = mateTransferService.mateTransferList(mateTransfer);
        return getDataTable(list);
    }

    /*
       查询已经做完的生产工序传递卡
*/

    @GetMapping("/selectMateTransfer")
    public MateTransfer selectMateTransfer(@RequestParam(value = "transferId", required = false) Long transferId) {
        MateTransfer mateTransfer = mateTransferService.selectMateTransfer(transferId);
        return mateTransfer;
    }

    /**
     * 删除生产工序传递卡
     */
    @Log(title = "删除生产工序传递卡", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{transferId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("transferId") Long transferId) {
        mateTransferService.remove(transferId);
        return AjaxResult.success("删除成功");
    }

    /*
         编辑生产工序传递卡
            */
    @PostMapping("/updateMateTransfer")
    @ResponseBody
    public AjaxResult updateMateTransfer(@RequestBody MateTransfer mateTransfer) {
        int num = mateTransferService.updateMateTransfer(mateTransfer);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    /*
    查询审核通过的检验项目和原材料信息
     */
    @GetMapping("/selectChildren/{transferId}")
    public Map<String, Object> selectChildren(@PathVariable("transferId") Long transferId) {
        Map<String, Object> resMap = new HashMap<>();
        List<MateTransferModel> mateTransferModels = mateTransferService.selectMateTransferModel(transferId);
        List<MateChildren> mateChildren = mateTransferService.selectChildren(transferId);
        resMap.put("mateTransferModels", mateTransferModels);
        resMap.put("mateChildren", mateChildren);
        return resMap;
    }

}
